package cn.frh.basic.service.impl;

import cn.frh.basic.mapper.BaseMapper;
import cn.frh.basic.query.BaseQuery;
import cn.frh.basic.service.IBaseService;
import cn.frh.basic.util.pageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public T loadById(Long Id) {
        return baseMapper.loadById(Id);
    }

    @Override
    @Transactional
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    @Transactional
    public pageList<T> queryPage(BaseQuery baseQuery) {
        Integer queryCount = baseMapper.queryCount(baseQuery);
        if (queryCount == 0){
            return new pageList<>();
        }
        List<T> list = baseMapper.queryPage(baseQuery);
        return new pageList<T>(queryCount,list);
    }

    @Override
    @Transactional
    public void patchRemove(List<Long> ids) {
        baseMapper.patchRemove(ids);
    }
}
